//
//  HLJTalkViewController.m
//  B段项目
//
//  Created by 111111 on 15/12/25.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "HLJTalkViewController.h"
#import "HLJTalkHotCell.h"
#import "HLJTalkHotAnotherCell.h"
#import "TalkDetailViewController.h"
#import "TOTools.h"
@interface HLJTalkViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, retain)UITableView *talkTableView;
@property (nonatomic, assign)BOOL isUpLoad;
@property (nonatomic, retain)NSMutableArray *talkHotArray;
@property (nonatomic, assign)NSInteger nextPage;
@property (nonatomic, assign)CGFloat cellHeight;

@property(nonatomic, retain)UISegmentedControl *segmentC;

@end

@implementation HLJTalkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   // self.view.backgroundColor = [UIColor grayColor];
    
   // [self getAFNetWorkingData];
   // [self getAFNetWorkingDataNew];
    // 分段按钮
    NSArray *array = [NSArray arrayWithObjects:@"Hot", @"New", nil];
   self.segmentC = [[UISegmentedControl alloc] initWithItems:array];
    self.segmentC.backgroundColor = [UIColor whiteColor];
    self.segmentC.frame = CGRectMake(WIDTH / 4, 10, WIDTH / 2, WIDTH / 2 / 7);
    // 设置按钮选中颜色
    self.segmentC.tintColor = [UIColor colorWithRed:0.42 green:0.69 blue:0 alpha:1];
    [self.view addSubview:self.segmentC];
        self.segmentC.selectedSegmentIndex = 0;
    // target Action
    [self.segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
   // self.talkTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, segmentC.frame.origin.y + segmentC.frame.size.height + 10, WIDTH, HEIGHT - self.talkTableView.origin.y - 110) style:UITableViewStylePlain];
    
    self.talkTableView = [[UITableView alloc]init];
    [self.view addSubview:self.talkTableView];
    
    [self.talkTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.size.mas_equalTo(CGSizeMake(self.view.size. width, self.view.size. height - 50) );
        
          }];
    
    self.talkTableView.delegate = self;
    self.talkTableView.dataSource = self;
   // [self.view addSubview:self.talkTableView];
    
    [self.talkTableView registerClass:[HLJTalkHotCell class] forCellReuseIdentifier:@"talkHotCell"];
    [self.talkTableView registerClass:[HLJTalkHotAnotherCell class] forCellReuseIdentifier:@"talkHotAnotherCell"];
    
    self.talkHotArray = [NSMutableArray array];
    self.isUpLoad = NO;
    
   MJRefreshGifHeader *header = [TOTools createTableViewGifHeaderWithTarget:self Action:@selector(addHeaderRefreshHot)];
    self.talkTableView.mj_header = header;
    [self.talkTableView.mj_header beginRefreshing];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addFooterRefreshHot)];
    self.talkTableView.mj_footer = footer;
    


}


#pragma mark- 数据解析
- (void)getAFNetWorkingData{
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"正在为您加载.....";
    [self.view addSubview:hub];
    
    NSString *urlStr = @"http://api2.pianke.me/group/posts_hotlist";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&sort=hot&start=%ld&version=3.0.6", self.nextPage];
    [ZYTools postUrl:urlStr body:para result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
      //  NSLog(@"%@",result);
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.talkHotArray removeAllObjects];
        }
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in [bigDic objectForKey:@"list"]) {
            HLJTalkHotModel *talkHot = [[HLJTalkHotModel alloc] init];
            [talkHot setValuesForKeysWithDictionary:dic];
            [self.talkHotArray addObject:talkHot];
            
        }
        [self.talkTableView.mj_header endRefreshing];
        [self.talkTableView.mj_footer endRefreshing];

               [self.talkTableView reloadData];
        
        [hub hide:YES];
    } failure:^(NSError *error) {
        [hub hide:YES];
        
    }];
}


#pragma mark- 数据解析(New)
- (void)getAFNetWorkingDataNew{
    NSString *urlStr = @"http://api2.pianke.me/group/posts_hotlist";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&sort=addtime&start=%ld&version=3.0.6", self.nextPage];
    [ZYTools postUrl:urlStr body:para result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
      //  NSLog(@"%@",result);
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.talkHotArray removeAllObjects];
        }
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in [bigDic objectForKey:@"list"]) {
            HLJTalkHotModel *talkHot = [[HLJTalkHotModel alloc] init];
            [talkHot setValuesForKeysWithDictionary:dic];
            [self.talkHotArray addObject:talkHot];
           
        }
        
        [self.talkTableView.mj_header endRefreshing];
        [self.talkTableView.mj_footer endRefreshing];

        [self.talkTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   // NSLog(@"%ld",self.talkHotArray.count);
    return self.talkHotArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *talkHotCellIdntifier = @"talkHotCell";
    HLJTalkHotCell *talkHotCell = [tableView dequeueReusableCellWithIdentifier:talkHotCellIdntifier];
    
    static NSString *talkHotAnotherIdntifier = @"talkHotAnotherCell";
    HLJTalkHotAnotherCell *talkHotAnotherCell = [tableView dequeueReusableCellWithIdentifier:talkHotAnotherIdntifier];
    
    // 取消点击cell的效果
    talkHotCell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 取消点击cell的效果
    talkHotAnotherCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    HLJTalkHotModel *talkHot = [self.talkHotArray objectAtIndex:indexPath.row];
    if ([talkHot.coverimg isEqualToString:@""]) {
        talkHotAnotherCell.talkAnotherModel = talkHot;
        
    //    self.cellHeight = talkHotAnotherCell.comImageView.frame.origin.y + talkHotAnotherCell.comImageView.frame.size.height + WIDTH/25;
        return talkHotAnotherCell;
    } else {
        talkHotCell.talkHotImg = talkHot;
        
       // self.cellHeight = talkHotCell.comImageView.frame.origin.y + talkHotCell.comImageView.frame.size.height + WIDTH/25;
        return talkHotCell;
    }
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   // NSLog(@"3333333333333333333");
    return 240;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TalkDetailViewController *detailVC = [[TalkDetailViewController alloc]init];
    HLJTalkHotModel *talkModel = [self.talkHotArray objectAtIndex:indexPath.row];

    detailVC.detaId = talkModel.contentid;
    detailVC.titleStr = talkModel.title;
    [self.navigationController pushViewController:detailVC animated:YES];
    
    
}

#pragma mark -- 设置下拉刷新
-(void)addHeaderRefreshHot
{//NSLog(@"下拉hot");
    self.nextPage = 0;
    self.isUpLoad = NO;//标记为下拉操作
    
    if (self.segmentC.selectedSegmentIndex == 0) {
        [self getAFNetWorkingData];
     //   NSLog(@"下拉hot");
    }else{
        [self getAFNetWorkingDataNew];
    }
    
    
}
#pragma mark -- 设置上拉刷新
-(void)addFooterRefreshHot
{
    self.nextPage += 10;
    self.isUpLoad = YES;
    
    if (self.segmentC.selectedSegmentIndex == 0) {
        [self getAFNetWorkingData];
       // NSLog(@"下拉hot");
    }else{
        [self getAFNetWorkingDataNew];
    }

}



#pragma mark -- 设置下拉刷新
-(void)addHeaderRefreshNew
{//NSLog(@"下拉new");
        self.nextPage = 0;
    self.isUpLoad = NO;//标记为下拉操作
    [self getAFNetWorkingDataNew];
    
}
#pragma mark -- 设置上拉刷新
-(void)addFooterRefreshNew
{
    self.nextPage += 10;
    self.isUpLoad = YES;
    [self getAFNetWorkingDataNew];
}


- (void)segmentAction:(UISegmentedControl *)segment{
    if (segment.selectedSegmentIndex == 0) {
#pragma mark- 调用上拉下拉方法
         self.segmentC.selectedSegmentIndex = 0;
        [self addHeaderRefreshHot];
        [self addFooterRefreshHot];
        self.isUpLoad = NO;
       
    } else {
#pragma mark- 调用上拉下拉方法
         self.segmentC.selectedSegmentIndex = 1;
        [self addHeaderRefreshNew];
        [self addFooterRefreshNew];
       
        self.isUpLoad = NO;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
